package com.yupi.springbootinit.bizmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

public class BIInitMq {

    public static void main(String[] argv) {
        try{
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(BIMqConstant.BI_MQ_EXCHANGE, "direct");
            channel.exchangeDeclare(BIMqConstant.DEAD_EXCHANGE_NAME, "direct");
            // 创建用于指定死信队列的参数的Map对象
            Map<String, Object> args = new HashMap<>();
            // 将要创建的队列绑定到指定的交换机，并设置死信队列的参数
            args.put("x-dead-letter-exchange", BIMqConstant.DEAD_EXCHANGE_NAME);
            // 指定死信要转发到外包死信队列
            args.put("x-dead-letter-routing-key", "dlx");
            //声明死信队列并绑定
            channel.queueDeclare(BIMqConstant.DEAD_QUEUE_NAME, true, false, false, null);
            channel.queueBind(BIMqConstant.DEAD_QUEUE_NAME, BIMqConstant.DEAD_EXCHANGE_NAME, "dlx");

            // 声明队列，设置队列为持久化的，非独占的，非自动删除的
            channel.queueDeclare(BIMqConstant.BI_MQ_QUEUE, true, false, false, args);
            // 将队列绑定到指定的交换机上，并指定绑定的路由键为 "xiaoyu"
            channel.queueBind(BIMqConstant.BI_MQ_QUEUE, BIMqConstant.BI_MQ_EXCHANGE, BIMqConstant.BI_MQ_ROUTING_KEY);
        } catch (Exception e) {

        }
    }
}
